import { PaginationDemo } from './demo'; import { Pagination, Link } from '@aws-amplify/ui-react'; import { Example, ExampleCode } from '@/components/Example'; import { ComponentStyleDisplay } from '@/components/ComponentStyleDisplay'; import ThemeExample from '@/components/ThemeExample.mdx'; import { ControlledPaginationExample, DefaultPaginationExample, PaginationAccessibilityExample, PaginationHasMorePagesExample, PaginationSiblingCountExample, PaginationStylingExample, PaginationThemeExample, } from './examples'; ## Demo ## Usage Import the Pagination component. To use Pagination as an uncontrolled component, import the `usePagination` hook and pass it an object including the following properties: - `totalPages` (required) - `currentPage` (optional, defaults to `1`) - `siblingCount` (optional, defaults to `1`) - `hasMorePages` (optional) ```tsx file=./examples/DefaultPaginationExample.tsx ``` ### Controlled component To use Pagination as a controlled component, you'll need to handle state using these callback functions: - `onNext`: triggered when the next-page button `>` is pressed - `onPrevious`: triggered when the previous-page button `<` is pressed - `onChange`: triggered every time the page changes (e.g., when a page button is pressed directly) ```tsx file=./examples/ControlledPaginationExample.tsx ``` ### Paginating at an API level It's common to use a paged API where the total number of pages in the dataset is unknown until you've reached the final page. To use the Pagination component in this scenario, set the `totalPages` prop to the total pages of content loaded from the API _so far_, and set `hasMorePages` initially to `true` (which enables the next button to be clicked to trigger another API call). Update the `totalPages` as you fetch more data. Once you reach the end of the data, set the `hasMorePages` to false (and thus the Pagination component's next button will be disabled). See below for a contrived example: ```tsx file=./examples/PaginationHasMorePagesExample.tsx ``` ### Sibling Count `siblingCount` is an optional prop which controls the number of pages displayed on each side of the current page (defaults to `1`). For example, in the demo below, notice how page 5 has two siblings to the left (3 and 4) and two siblings to the right (6 and 7). ```tsx{7} file=./examples/PaginationSiblingCountExample.tsx ``` ## Accessibility Accessible labels are provided for the Pagination buttons either via the `aria-label` attribute or by using the [VisuallyHidden](/react/components/visuallyhidden) primitive. Use the following props to customize these labels: - **`previousLabel`**: Set the `aria-label` for the previous page button (defaults to `Go to previous page`) - **`nextLabel`**: Set the `aria-label` for the next page button (defaults to `Go to next page`) - **`currentPageLabel`**: Set the `VisuallyHidden` label for current page (defaults to `Page`). This will be used to construct the label text for current page. e.g, `Page: 1` if page `1` is the current page. - **`pageLabel`**: Set the label for each page button other than the current page (defaults to `Go to page`). This will be used to construct the `aria-label`. e.g, `Go to page 1` for page `1` button. By default, the root node of the Pagination component is a `